#include "stdafx.h"
#include "UIAnimation.h"
#include "UICollections.h"
#include "UIElement.h"
#include "UIPage.h"


namespace DirectUI
{
	namespace UIAnimation
	{
		IUIElement* GetElementTemplateObject()
		{
			static CUIObjectPtr<IUIElement> _theElementTemplateObject;
			if ( !_theElementTemplateObject )
				_theElementTemplateObject = new CUIElement();
			return _theElementTemplateObject;
		}

		CUIStoryboard::CUIStoryboard()
		{

		}

		CUIStoryboard::~CUIStoryboard()
		{
		}

		void CUIStoryboard::Begin(IUIElement* lpStateOwner)
		{
			assert(lpStateOwner);
			if ( !lpStateOwner )
				return;

			std::vector<CUIObjectPtr<IUITimeline>>::iterator pit = m_vChildrens.begin();
			for ( ; pit != m_vChildrens.end(); ++pit )
			{
				CUIObjectPtr<IUITimeline> spTimeline = *pit;
				CUITimeline<IUITimeline, CUITimeline, IUITimeline>* p = (CUITimeline<IUITimeline, CUITimeline, IUITimeline>*)(IUITimeline*)spTimeline;
				p->Animate(lpStateOwner);
			}
		}

		void CUIStoryboard::Stop()
		{
			std::vector<CUIObjectPtr<IUITimeline>>::iterator pit = m_vChildrens.begin();
			for ( ; pit != m_vChildrens.end(); ++pit )
			{
				CUIObjectPtr<IUITimeline> spTimeline = *pit;
				CUITimeline<IUITimeline, CUITimeline, IUITimeline>* p = (CUITimeline<IUITimeline, CUITimeline, IUITimeline>*)(IUITimeline*)spTimeline;
				p->StopAnimate();
			}
		}

		HRESULT CUIStoryboard::QueryInterface( const GUID& guid, void** lppObject )
		{
			if ( guid == __uuidof(IUIStoryboard) )
			{
				if (lppObject)
				{
					*lppObject = (IUIStoryboard*)this;
					AddRef();
				}
				return S_OK;
			}

			return __super::QueryInterface(guid, lppObject);
		}

		void CUIStoryboard::AddChild( IUITimeline* lpChild )
		{
			assert(lpChild);
			if ( !lpChild )
				return;
			m_vChildrens.push_back(lpChild);
		}
	}
}
